//封装请求的方法
import axios from 'axios';
import { Notification } from "element-ui";
//加载进度条
import NProgress from 'nprogress';
import 'nprogress/nprogress.css'
import Router from "../router/index";

import config from '@/config'
//初始化一个
var instance = axios.create({
    //接口域名
    // baseURL: 'http://139.224.55.204/platformapi/uc',
    // baseURL: 'http://106.54.54.254/platform/uc',
    // baseURL: 'http://localhost:8090/uc',
    baseURL: config.baseURL,
    //接口超时时间 20秒
    timeout: 30000
});



//请求之前做的事情
instance.interceptors.request.use(
    function(config){
        //设置token
        config.headers['token'] = window.localStorage.getItem('token');
        //请求进度条
        // NProgress.start();
        return config;
    },
    function(error){
        // NProgress.done();
        return Promise.reject(error);
    }
);


//请求完成做的事情
instance.interceptors.response.use(
    function(response){
        //
        NProgress.done();
        let status =  response.data.status;
        if(status != null && status !== '200'){
            Notification.error(response.data.message);
        }
        if(status != null && (status === '401')){
            Router.push({path:'/login'})
        }
        return response;
    },
    function(error){
        NProgress.done();
        let status =  error.response.data.status;
        if(status != null && (status === '401')){
            Router.push({path:'/login'})
        }
        return Promise.reject(error);
    }
);

let  get = async function (url,params) {
    let {data} =  await instance.get(url,{params});
    return data;
}

let post = async function(url,params){
    let {data} = await instance.post(url,params);
    return data;
}

//导出get post方法
export {
    get,
    post
}